package controllers

import (
	"cdam/models"
	"cdam/util"
	"encoding/json"
	"errors"
	"math/rand"
	"strconv"
	"strings"
	"time"

	"github.com/astaxie/beego"
)

// TenantController operations for Tenant
type TenantController struct {
	beego.Controller
}

// URLMapping ...
func (c *TenantController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
	c.Mapping("BatchDelete",c.BatchDelete)
}

// BatchDelete
// @Title Post
// @Description batch delete tenant
// @Param	body		body 	models.Tenant	true		"body for Tenant content"
// @Success 201 {int} models.Tenant
// @Failure 403 body is empty
// @router /BatchDelete [post]
func (c *TenantController) BatchDelete()  {
	m := map[string]interface{}{}
	var body []int64
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &body); err == nil {
		err := models.BatchDelete(body)
		if err == nil {
			m["code"] = "20000"
		}else{
			m["code"] = "80000"
			m["message"] = err.Error()
		}
	}
	c.Data["json"] = m
	c.ServeJSON()
}

// Post ...
// @Title Post
// @Description create Tenant
// @Param	body		body 	models.Tenant	true		"body for Tenant content"
// @Success 201 {int} models.Tenant
// @Failure 403 body is empty
// @router / [post]
func (c *TenantController) Post() {
	var v models.Tenant
	m := map[string]interface{}{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		v.Id = int(rand.New(util.Seed).Int63n(100000000))
		v.Createdate = time.Now()
		lst,err := models.GetTenantByCondition(&v)
		if len(lst) >0 || err != nil {
			if err != nil {
				m["code"] = "80000"
				m["message"] = err.Error()
			}else if len(lst) > 0 {
				m["code"] = "80000"
				m["message"] = "租户编码或者租户名称不唯一!"
			}
		}else{
			if _, err := models.AddTenant(&v); err == nil {
				c.Ctx.Output.SetStatus(201)
				m["code"] = "20000"
				// c.Data["json"] = v
			} else {
				m["code"] = "80000"
				m["message"] = err.Error()
			}
		}
	} else {
		m["code"] = "80000"
		m["message"] = err.Error()
	}
	c.Data["json"] = m
	c.ServeJSON()
}

// GetOne ...
// @Title Get One
// @Description get Tenant by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Tenant
// @Failure 403 :id is empty
// @router /:id [get]
func (c *TenantController) GetOne() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	v, err := models.GetTenantById(id)
	var m = make(map[string]interface{})
	if err != nil {
		m["code"] = "80000"
		m["message"] = err.Error()
	} else {
		m["code"] = "20000"
		m["tenant"] = v
	}
	c.Data["json"] = m
	c.ServeJSON()
}

// GetAll ...
// @Title Get All
// @Description get Tenant
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.Tenant
// @Failure 403
// @router / [get]
func (c *TenantController) GetAll() {
	var fields []string
	var sortby []string
	var order []string
	var query = make(map[string]string)
	var limit int64 = 10
	var offset int64
	m := map[string]interface{}{}
	// fields: col1,col2,entity.col3
	if v := c.GetString("fields"); v != "" {
		fields = strings.Split(v, ",")
	}
	// limit: 10 (default is 10)
	if v, err := c.GetInt64("limit"); err == nil {
		limit = v
	}
	// offset: 0 (default is 0)
	if v, err := c.GetInt64("offset"); err == nil {
		offset = v
	}
	// sortby: col1,col2
	if v := c.GetString("sortby"); v != "" {
		sortby = strings.Split(v, ",")
	}
	// order: desc,asc
	if v := c.GetString("order"); v != "" {
		order = strings.Split(v, ",")
	}
	// query: k:v,k:v
	if v := c.GetString("query"); v != "" {
		for _, cond := range strings.Split(v, ",") {
			kv := strings.SplitN(cond, ":", 2)
			if len(kv) != 2 {
				c.Data["json"] = errors.New("Error: invalid query key/value pair")
				c.ServeJSON()
				return
			}
			k, v := kv[0], kv[1]
			query[k] = v
		}
	}

	l, count, err := models.GetAllTenant(query, fields, sortby, order, offset, limit)
	if err != nil {
		m["code"] = "80000"
		m["message"] = err.Error()
	} else {
		m["code"] = "20000"
		m["data"] = l
		m["count"] = count
	}
	c.Data["json"] = m
	c.ServeJSON()
}

// Put ...
// @Title Put
// @Description update the Tenant
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.Tenant	true		"body for Tenant content"
// @Success 200 {object} models.Tenant
// @Failure 403 :id is not int
// @router /:id [put]
func (c *TenantController) Put() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	v := models.Tenant{Id: id}
	m := map[string]interface{}{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
    v.Updatedate = time.Now()
    if err := models.UpdateTenantById(&v); err == nil {
      m["code"] = "20000"
    } else {
      m["code"] = "80000"
      m["message"] = err.Error()
    }
	} else {
		m["code"] = "80000"
		m["message"] = err.Error()
	}
	c.Data["json"] = m
	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the Tenant
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *TenantController) Delete() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	m := map[string]interface{}{}
	if err := models.DeleteTenant(id); err == nil {
		m["code"] = "20000"
	} else {
		m["code"] = "80000"
		m["message"] = err.Error()
	}
	c.Data["json"] = m
	c.ServeJSON()
}
